/*
* Copyright (C) 2020 ~ 2024 chaigec All Rights Reserved.
*
* Author:     chaigec <chaigec@163.com>
* Maintainer: chaigec <chaigec@163.com>

 * Use of this source code is governed by MIT license that can be found in the
 * LICENSE file in the root of the source tree. All contributing project authors
 * may be found in the AUTHORS file in the root of the source tree.
*/

#include "MkMedia/MkFileSource.h"
#include "MkRtmp/MkRtmpPublisher.h"
//#include "MkLibRtmp/MkRtmpSender.h"
#include "MkEvent/MkEvent.h"
#include "MkEvent/MkEventPool.h"
#include "MkEvent/MkTimer.h"
#include "MkUtil/MkLog.h"
#include <signal.h>

CMkFileSource *g_pFileSource = nullptr;
CMkRtmpPublisher *g_pPublisher = nullptr;
CMkTimer *g_pTimer = nullptr;
CMkEventPool* g_pPool = nullptr;

void SigHandle(int sig)
{
    if (sig == SIGINT) {
        g_pTimer->LoopBreak();
    }
};

void RtmpPublish264()
{
    //FILE *fp = fopen("C:/Workspace/media/aa.265", "rb");
    FILE *fp = fopen("D:/mediaTest/aa.264", "rb");
    CMkEventPool Pool;
    g_pPool = &Pool;
    Pool.Start(1);
    CMkTimer Timer;
    g_pTimer = &Timer;
    MkString RtmpUrl = "rtmp://127.0.0.1/live/264";
    g_pFileSource = new CMkH264FileSource(fp, g_pTimer);
    g_pPublisher = new CMkRtmpPublisher(Pool.GetFreeEvent(), g_pTimer, [fp](CMkTcpClient* pClient) {
        MkDelete(g_pFileSource);
        fclose(fp);
        MkDelete(pClient);
        g_pPublisher = nullptr;
        g_pTimer->LoopBreak();
    });
    g_pPublisher->Start(RtmpUrl, g_pFileSource->GetVideoParam(), g_pFileSource->GetAudioParam(), g_pFileSource->m_FileSize, g_pFileSource->m_Duration, []() {
        g_pFileSource->StartMediaSource([](CMkMediaSource* pMediaSource, Uint32 ErrorCode) {
            return NoneError;
        }, [](const MkEsPacket& EsPacket, const CMkVideoParam& VideoParam, const CMkAudioParam& AudioParam) {
            g_pPublisher->SendEsPacket(EsPacket);
            // MkDebugLog("espacket:%d\n",EsPacket.BufferList.GetBufferList().size());
            return NoneError;
        });
    });
    Timer.Dispatch();
    if (g_pPublisher) {
        g_pPublisher->Close();
        CMkThread::ThreadSleep(1000);
    }
    Pool.Stop();
}

void RtmpPublish265()
{
    //FILE *fp = fopen("C:/Workspace/media/aa.265", "rb");
    FILE *fp = fopen("D:/mediaTest/aa.265", "rb");
    CMkEventPool Pool;
    g_pPool = &Pool;
    Pool.Start(1);
    CMkTimer Timer;
    g_pTimer = &Timer;
    MkString RtmpUrl = "rtmp://127.0.0.1/live/test";
    g_pFileSource = new CMkH265FileSource(fp, g_pTimer);
    g_pPublisher = new CMkRtmpPublisher(Pool.GetFreeEvent(), g_pTimer, [fp](CMkTcpClient* pClient) {
        MkDelete(g_pFileSource);
        fclose(fp);
        MkDelete(pClient);
        g_pPublisher = nullptr;
        g_pTimer->LoopBreak();
    });
    g_pPublisher->Start(RtmpUrl, g_pFileSource->GetVideoParam(), g_pFileSource->GetAudioParam(), g_pFileSource->m_FileSize, g_pFileSource->m_Duration, []() {
        g_pFileSource->StartMediaSource([](CMkMediaSource* pMediaSource, Uint32 ErrorCode) {
            return NoneError;
        }, [](const MkEsPacket& EsPacket, const CMkVideoParam& VideoParam, const CMkAudioParam& AudioParam) {
            g_pPublisher->SendEsPacket(EsPacket);
            // MkDebugLog("espacket:%d\n",EsPacket.BufferList.GetBufferList().size());
            return NoneError;
        });
    });
    Timer.Dispatch();
    if (g_pPublisher) {
        g_pPublisher->Close();
        CMkThread::ThreadSleep(1000);
    }
    Pool.Stop();
}

//void RtmpPublish264()
//{
//    CMkTimer Timer;
//    g_pTimer = &Timer;
//    FILE *fp = fopen("C:/Workspace/media/aa.264", "rb");
//    MkString RtmpUrl = "rtmp://192.168.100.120:51935/live/test1";
//    CMkRtmpSender *pSender = CMkRtmpSender::CreateNew(RtmpUrl);
//    g_pFileSource = new CMkH264FileSource(fp, g_pTimer, [pSender](const MkEsPacket& EsPacket, const CMkVideoParam& VideoParam, const CMkAudioParam& AudioParam) {
//        pSender->SetMediaParam(pMediaSource->GetVideoParam(), pMediaSource->GetAudioParam());
//        pSender->SendEsPacket(EsPacket);
//    }, [](CMkMediaSource* pMediaSource, Uint32 ErrorCode) {
//
//    });
//    g_pFileSource->ScanFile();
//    g_pFileSource->Start();
//    Timer.Dispatch();
//}

int main(int argc, char ** argv)
{
#ifdef _WIN32
    WSADATA WSAData;
    WSAStartup(0x101, &WSAData);
#else
    //signal(SIGPIPE, SIG_IGN);  //ignore sigpipe
    //signal(SIGPIPE, SIG_IGN);
    sigset_t set;
    sigemptyset(&set);
    sigaddset(&set, SIGPIPE);
    sigprocmask(SIG_BLOCK, &set, NULL);
#endif
    MkLogChangeLevel(LOG_DEBUG);
    signal(SIGINT, SigHandle);
    RtmpPublish264();
    //TestLibrtmpPublisher();
#ifdef _WIN32
    WSACleanup();
#endif
    return 0;
}